package com.sqx.modules.fiction.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.fiction.dao.FictionChapterDao;
import com.sqx.modules.fiction.dao.FictionDao;
import com.sqx.modules.fiction.entity.NFiction;
import com.sqx.modules.fiction.entity.NFictionChapter;
import com.sqx.modules.fiction.entity.dto.AddChapterDto;
import com.sqx.modules.fiction.service.FictionChapterService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class FictionChapterServiceImpl  extends ServiceImpl<FictionChapterDao, NFictionChapter> implements FictionChapterService {
    @Autowired
    private FictionChapterDao fictionChapterDao;
    @Autowired
    private FictionDao fictionDao;

    @Override
    @Transactional
    public Result addChapter(AddChapterDto addChapterDto) {
        NFictionChapter fictionChapter = new NFictionChapter();
        fictionChapter.setFictionId(addChapterDto.getFictionId());
        fictionChapter.setFictionChapterName(addChapterDto.getFictionChapterName());
        fictionChapter.setFictionChapterText(addChapterDto.getFictionChapterText());
        fictionChapter.setCreateTime(new Date());
        fictionChapter.setFictionChapterPicture(addChapterDto.getFictionChapterPicture());
        fictionChapter.setFictionChapterToll(addChapterDto.getFictionChapterToll());
        fictionChapter.setFictionChapterPrice(addChapterDto.getFictionChapterPrice());
        fictionChapter.setFictionChapterIntro(addChapterDto.getFictionChapterIntro());
        fictionChapter.setFictionDelStatus(1);
        fictionChapter.setFictionChapterWordTotal(addChapterDto.getFictionChapterText().length());
        fictionChapterDao.insert(fictionChapter);
        NFiction fiction = fictionDao.selectById(fictionChapter.getFictionId());
        fiction.setFictionWordTotal(fiction.getFictionWordTotal()+fictionChapter.getFictionChapterWordTotal());
        fictionDao.updateById(fiction);
        return Result.success("添加成功");
    }

    @Override
    public Result updateChapter(NFictionChapter addChapterDto) {
        int textWordTotal = addChapterDto.getFictionChapterText().length();
        NFictionChapter chapter = fictionChapterDao.selectById(addChapterDto.getFictionChapterId());
        NFiction fiction = fictionDao.selectById(chapter.getFictionId());
        int wordTotal = fiction.getFictionWordTotal() - chapter.getFictionChapterWordTotal();
        NFictionChapter fictionChapter = new NFictionChapter();
        BeanUtil.copyProperties(addChapterDto,fictionChapter);
        fictionChapter.setFictionChapterWordTotal(textWordTotal);
        fictionChapterDao.updateById(fictionChapter);
        fiction.setFictionWordTotal(wordTotal+textWordTotal);
        fictionDao.updateById(fiction);
        return Result.success("修改成功");
    }

    @Override
    public Result deleteChapter(Long id) {
        NFictionChapter fictionChapter = fictionChapterDao.selectById(id);
        fictionChapter.setFictionDelStatus(0);
        fictionChapter.setDeleteTime(new Date());
        fictionChapterDao.updateById(fictionChapter);
        return Result.success();
    }

    @Override
    public Result getChapterById(Long id) {
        NFictionChapter fictionChapter = fictionChapterDao.selectById(id);
        return Result.success().put("data",fictionChapter);
    }

    @Override
    public Result getChapterByFictionId(Long fictionId,Integer page,Integer limit) {
        Page<NFictionChapter> chapterPage = new Page<>(ObjectUtils.isEmpty(page)?1:page,ObjectUtils.isEmpty(limit)?10:limit);
        LambdaQueryWrapper<NFictionChapter> chapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chapterLambdaQueryWrapper.eq(NFictionChapter::getFictionId,fictionId);
        chapterLambdaQueryWrapper.eq(NFictionChapter::getFictionDelStatus,1);
        chapterLambdaQueryWrapper.orderByDesc(NFictionChapter::getFictionChapterSort);
        IPage<NFictionChapter> chapterIPage = fictionChapterDao.selectPage(chapterPage,chapterLambdaQueryWrapper);
        return Result.success().put("data",new PageUtils(chapterIPage));
    }

    @Override
    public Result getChapterList(Long fictionId,Integer page,Integer limit) {
        Page<NFictionChapter> chapterPage = new Page<>(ObjectUtils.isEmpty(page)?1:page,ObjectUtils.isEmpty(limit)?10:limit);
        LambdaQueryWrapper<NFictionChapter> chapterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chapterLambdaQueryWrapper.eq(NFictionChapter::getFictionId,fictionId);
        chapterLambdaQueryWrapper.eq(NFictionChapter::getFictionDelStatus,1);
        chapterLambdaQueryWrapper.orderByDesc(NFictionChapter::getFictionChapterSort);
        IPage<NFictionChapter> chapterIPage = fictionChapterDao.selectPage(chapterPage,chapterLambdaQueryWrapper);
        return Result.success().put("data",new PageUtils(chapterIPage));
    }

    @Override
    public Result deleteChapterById(Long fictionChapterId) {
        LambdaUpdateWrapper<NFictionChapter> chapterLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        chapterLambdaUpdateWrapper.eq(NFictionChapter::getFictionChapterId,fictionChapterId);
        chapterLambdaUpdateWrapper.set(NFictionChapter::getFictionDelStatus,0);
        chapterLambdaUpdateWrapper.set(NFictionChapter::getDeleteTime,new Date());
        fictionChapterDao.update(null,chapterLambdaUpdateWrapper);
        return Result.success();
    }
}
